package cgl.narada.test.security;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import cgl.narada.service.security.impl.EntityOperationsImpl;

public class SigningTester {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String s = "AES/CBC/PKCS7Padding"; ///CBC/PKCS#5";
        String sigAlgo = "SHA512WithRSAEncryption";
        
        try {
            KeyGenerator kg = KeyGenerator.getInstance("AES");
            // kg.init();
            kg.init(256, new SecureRandom());

            SecretKey secureTopicKey = kg.generateKey();

            System.out.println(secureTopicKey.getFormat());

            String z = "Hello World";
            EntityOperationsImpl eoi = new EntityOperationsImpl();
            byte[] iv = eoi.getIV("AES", "CBC", "PKCS7Padding");
            
            byte[] enc = eoi.encryptPayload(secureTopicKey, z.getBytes(), s, iv, "BC");

            byte[] org = eoi.decryptPayload(secureTopicKey, enc, s, iv, "BC");
            System.out.println(new String(org));
            
            
            // Sig stuffff....
            
            // Generate pub/priv
            // Generate a 1024-bit Digital Signature Algorithm (DSA) key pair
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
            keyGen.initialize(1024);
            KeyPair keypair = keyGen.genKeyPair();
            PrivateKey privateKey = keypair.getPrivate();
            PublicKey publicKey = keypair.getPublic();
                   
            byte[] signature = eoi.signPayload(privateKey, sigAlgo, "BC", enc);
            
            boolean validate = eoi.validateSignature(publicKey, sigAlgo, "BC", enc, signature);
            
            System.out.println("VALIDATE: " + validate);
            
            
           
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }

    }

}
